package com.maykk.Demo05;

import java.sql.*;

/**
 * @ClassName: JdbcUtilsSing
 * @Description: TODO
 * @Author: MaykkStar@163.com
 */
public class JdbcUtilsSing {
    private static final String URL = "jdbc:mysql://localhost:3306/maykk?useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    static {
        System.setProperty("jdbc.Driver", "com.mysql.jdbc.Driver");
    }

    /**
     * 显示单次实例化
     */
    private static JdbcUtilsSing instance = null;

    public static JdbcUtilsSing getInstance() {
        if (instance == null) {
            instance = new JdbcUtilsSing();
        }
        return instance;
    }

    /**
     * 获取单次实例化
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源
     *
     * @param rs   查询结果
     * @param st   状态
     * @param conn 连接对象
     */
    public void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (null != rs) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != st) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != conn) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
